package com.huadao.cart_ccs.experiment.adjustTemperature;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.ParamsPreferences;
import com.huadao.cart_ccs.common.deviceController.TemperatureController;
import com.huadao.cart_ccs.experiment.adjustWeight.AdjustWeightPresenter;

import butterknife.BindView;
import butterknife.OnClick;

public class AdjustTemperatureDF extends BaseDialogFragment implements IAdjustTemperatureContract.IAdjustTemperatureView {
    @BindView(R.id.et_adjust)
    EditText etAdjust;

    private AdjustTemperaturePresenter adjustTemperaturePresenter;

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_adjusttemperature;
    }

    @Override
    protected void onInit() {
        adjustTemperaturePresenter = new AdjustTemperaturePresenter(this);
        String adjust=String.valueOf(ParamsPreferences.getInstance().getTemperatureAdjust());
        etAdjust.setText(adjust);
        etAdjust.setSelection(adjust.length());
    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @OnClick({R.id.btn_adjust, R.id.btn_close,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                this.dismiss();
                break;
            case R.id.btn_adjust:
                String err = checkParams();
                if (!TextUtils.isEmpty(err)) {
                    showDialogMessage("", err);
                    return;
                }
                adjustTemperaturePresenter.adjust();
                break;
        }
    }

    private String checkParams() {
        String err = null;
        if (TextUtils.isEmpty(etAdjust.getText())) {
            err = "请输入校准值";
        }
        return err;
    }

    @Override
    public void onAdjust(double adjust) {
        //先前的设置值
        Double settedT=TemperatureController.getInstance().getSettedValue();
        if(settedT==null){
            settedT=0.0;
        }
        TemperatureController.getInstance().adjust(adjust);
        showToast("温度校准成功！");
        //记录校准值
        ParamsPreferences.getInstance().setTemperatureAdjustCommit(adjust);

        adjustTemperaturePresenter.setTemperature(settedT);
        adjustTemperaturePresenter.readTemperature();
        this.dismiss();
    }

    @Override
    public double getAdjust() {
        return Double.parseDouble(etAdjust.getText().toString());
    }
}
